<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="80px">
      <el-form-item label="会员ID" prop="memberUserId">
        <el-input
          v-model="queryParams.memberUserId"
          placeholder="请输入会员ID"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="短视频ID" prop="shortId">
        <el-input
          v-model="queryParams.shortId"
          placeholder="请输入短视频ID"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="收藏时间">
        <el-date-picker
          v-model="dateRange"
          value-format="YYYY-MM-DD"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['member:collection:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['member:collection:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="collectionList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="收藏ID" align="center" prop="memberCollectionId" width="100" />
      <el-table-column label="会员信息" align="center" width="150">
        <template #default="scope">
          <div style="display: flex; align-items: center; justify-content: center;">
            <el-avatar :size="30" :src="scope.row.memberAvatar || ''" style="margin-right: 8px;">
              <el-icon><UserFilled /></el-icon>
            </el-avatar>
            <span>{{ scope.row.memberNickname || `会员${scope.row.memberUserId}` }}</span>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="短视频封面" align="center" prop="shortPoster" width="100">
        <template #default="scope">
          <el-image
            style="width: 60px; height: 80px"
            :src="scope.row.shortPoster"
            :preview-src-list="[scope.row.shortPoster]"
            fit="cover"
            v-if="scope.row.shortPoster"
          >
            <template #error>
              <div class="image-slot">
                <el-icon><VideoPlay /></el-icon>
              </div>
            </template>
          </el-image>
        </template>
      </el-table-column>
      <el-table-column label="短视频ID" align="center" prop="shortId" width="100" />
      <el-table-column label="短视频描述" align="center" prop="shortDescription" :show-overflow-tooltip="true" />
      <el-table-column label="备注" align="center" prop="remark" :show-overflow-tooltip="true" />
      <el-table-column label="收藏时间" align="center" prop="createTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="100">
        <template #default="scope">
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['member:collection:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script setup name="Collection">
import { listCollection, delCollection } from "@/api/member/collection";
import { getCurrentInstance, ref, reactive, toRefs } from 'vue';
import { UserFilled, VideoPlay } from '@element-plus/icons-vue';

const { proxy } = getCurrentInstance();

const collectionList = ref([]);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const multiple = ref(true);
const total = ref(0);
const dateRange = ref([]);

const data = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    memberUserId: null,
    shortId: null
  }
});

const { queryParams } = toRefs(data);

/** 查询收藏列表 */
function getList() {
  loading.value = true;
  const params = proxy.addDateRange(queryParams.value, dateRange.value);
  listCollection(params).then(response => {
    collectionList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  dateRange.value = [];
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.memberCollectionId);
  multiple.value = !selection.length;
}

/** 删除按钮操作 */
function handleDelete(row) {
  const memberCollectionIds = row.memberCollectionId || ids.value;
  proxy.$modal.confirm('是否确认删除收藏编号为"' + memberCollectionIds + '"的数据项？').then(function() {
    return delCollection(memberCollectionIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('member/collection/export', {
    ...queryParams.value
  }, `member_collection_${new Date().getTime()}.xlsx`)
}

getList();
</script>

<style scoped>
.image-slot {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 100%;
  height: 100%;
  background: #f5f7fa;
  color: #909399;
  font-size: 20px;
}
</style>